﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Common;
using Basic.Model.Entity;
using Basic.Model.EntityManager;

namespace Basic.Business
{
    public class DictionaryAdmin
    {
        /// <summary>
        /// 添加一个字典明细到字典
        /// </summary>
        /// <param name="dictionaryTypeId"></param>
        /// <param name="dictionaryOrderNumber"></param>
        /// <param name="dictionaryValue"></param>
        public static void AddDictionary(string dictionaryTypeName, int dictionaryOrderNumber, string dictionaryValue)
        {
            ISession session = null;
            ITransaction trans = null;
            DictionaryType dictionaryType = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();

                dictionaryType = DictionaryTypeManager.GetByCondition(session, dictionaryTypeName, false);
                if (dictionaryType == null)
                    throw new Exception("当前字典不存在或者已经被删除。");
                Dictionary dictionary = new Dictionary();
                dictionary.dictionaryOrderNumber = dictionaryOrderNumber;
                dictionary.dictionaryValue = dictionaryValue;
                dictionary.dictionaryType = dictionaryType;
                DictionaryManager.Add(session, dictionary);

                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }

        /// <summary>
        /// 更新一个字典明细
        /// </summary>
        /// <param name="dictionaryId"></param>
        /// <param name="dictionaryOrderNumber"></param>
        /// <param name="dictionaryValue"></param>
        public static void UpdateDictionary(int dictionaryId, int dictionaryOrderNumber, string dictionaryValue)
        {
            ISession session = null;
            ITransaction trans = null;
            Dictionary dictionary = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();

                dictionary = DictionaryManager.Get(session, dictionaryId);
                if (dictionary == null)
                    throw new Exception("当前字典明细不存在或者已经被删除。");
                dictionary.dictionaryOrderNumber = dictionaryOrderNumber;
                dictionary.dictionaryValue = dictionaryValue;

                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
    }
}
